var { WebSocketServer } = require('ws');
const wss1 = new WebSocketServer({ noServer: true });
var clients=new Array();
wss1.on('connection',function(ws,request,socketKey){
        const ip = request.connection.remoteAddress;
        //const ip2 = request.headers['x-forwarded-for'].split(/\s*,\s*/)[0];
        var headers=JSON.stringify(request.headers);
        console.log(`connection,ip:${ip},headers:${headers}`); 
        ws.isAlive=true;
        ws.ip=ip;
        ws.keyId=socketKey;
        ws.on('error', console.error);
        ws.on('pong',heartbeat);
        ws.on('message',(data)=>{    
            ws.send('得到会话order：'+data);
            console.log(`Received message ${data} from user ${request}`);
        });
        clients.push(ws);
});
const interval = setInterval(function ping(){
    wss1.clients.forEach((client)=>{
        if(client.isAlive === false) {
            return client.terminate();
        }
        client.isAlive =false;
        client.ping();
    })
},30000);
function heartbeat(){
    this.isAlive=true;
 }
 wss1.on('close', function close() {
    clearInterval(interval);
  });
module.exports=wss1;